<template>
  <view
    class="home-container safe-area-inset-bottom"
    :style="'padding-top:' + barstatusH + 'px'"
  >
    <!-- 导航栏-派发对象 -->
    <view class="top-status-container" :style="'height:' + barstatusH + 'px'">
      <image
        class="bar-bg"
        :src="home_bar_bg"
        mode="scaleToFill"
        :style="'height:' + barstatusH + 'px'"
      ></image>
      <view class="top-status-paidx">
        <image class="paifa-icon" :src="home_tab_title"></image>
        <!-- <view class="top-status-paidx-title">实名制脱单 匹配精准度99.5%+</view> -->
      </view>
    </view>
    <view class="home-content-container">
      <!-- <scroll-view class="scroll-view_H" scroll-y="true"> -->
      <!-- 倒计时 牵线师 发对象 -->
      <faduixiangView
        :activityInfoMap="activityInfoMap"
        :isLogin="isLogin"
        :shareCountInfo="shareCountInfo"
        :paiCount="getPaiCount()"
        v-on:decryptPhoneNumber="decryptPhoneNumber"
        v-on:timeup="timeup"
        v-on:onzhuanfaAction="zhuanfaAction"
        v-on:onBaoMingAction="baomingAction"
        v-on:onstopPaiAction="stopPaiAction"
      ></faduixiangView>
      <taPiPei :dataList="taList"></taPiPei>
      <youPiPei
        v-if="showMath()"
        :mathData="mathData"
        :isLogin="isLogin"
        v-on:decryptPhoneNumber="decryptPhoneNumber"
      ></youPiPei>
    </view>
    <TabBar :current-page="0" />
  </view>
</template>

<script>
import { getInfo } from "@/api/user/user.js";
import {
  activityInfo,
  activityList,
  matchList,
  activityShareInfo,
  activityShare,
  activityApply,
} from "@/api/home/home.js";
import wxKeyManager from "@/util/getWXKey.js";
import { getAgesYear } from "@/util/tool";
import { EventBus } from "@/util/event-bus.js";
import { home_bar_bg, paifa_icon, home_tab_title } from "./home_image_tool.js";
/// 发对象
import faduixiangView from "@/pages/home2/components/faduixiang.vue";
/// 发对象
import taPiPei from "@/pages/home2/components/ta_pipei.vue";
/// 为你对象
import youPiPei from "@/pages/home2/components/you_pipei.vue";
export default {
  components: {
    faduixiangView,
    taPiPei,
    youPiPei,
  },
  onHide() {},
  data() {
    return {
      barstatusH: 0,
      isShowLogin: true,
      isLogin: false,
      wxcode: "",
      activityInfoMap: {},
      taList: [],
      mathData: {},
      shareCountInfo: {},
      home_bar_bg,
      paifa_icon,
      home_tab_title,
    };
  },
  created() {
    if (uni.getStorageSync("token")) {
      this.isLogin = true;
    }
    let that = this;
    EventBus.$on("sharePersonSuccess", function (res) {
      that.requestData();
    });
    EventBus.$on("didCustomize", function (res) {
      that.requestData();
    });
    EventBus.$on("didComplete", function (res) {
      that.requestData();
    });
    EventBus.$on("vipPaySuccess", function (res) {
      that.requestData();
    });
    EventBus.$on("realNameSuccess", function (res) {
      that.requestData();
    });
  },
  onLoad() {
    this.getHeight();
    this.requrstWxCode();
  },

  mounted() {},
  onShow() {
    this.requestData();
  },
  onShareAppMessage() {
    let that = this;
    let userInfo = getApp().globalData.userInfo;
    let shareTitle = that.getShareTitle(userInfo);

    let url = "/pages/home2/detail2/detail2?user_id=" + this.user_id;
    const promise = new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          title: shareTitle,
          path: url,
          imageUrl: userInfo.avatar,
        });
      }, 2000);
    });

    return {
      title: shareTitle,
      path: url,
      imageUrl: userInfo.avatar,
      promise,
    };
  },
  methods: {
    requestData() {
      this.refreshUserInfo();
      this.requestActivityInfo();
      this.requestActivityList();
      this.requestMatchList();
      this.requestShareInfo();
    },
    refreshUserInfo() {
      if (!this.isLogin) return;
      getInfo().then((res) => {
        if (res.status == 1) {
          uni.setStorageSync("userInfo", res.data || {});
          let userInfo = Object.assign({}, res.data);
          getApp().globalData.userInfo = userInfo;
        }
      });
    },
    requrstWxCode() {
      if (!this.isLogin) {
        let that = this;
        uni.login({
          provider: "weixin",
          success: function (loginRes) {
            that.wxcode = loginRes.code;
            console.log("weixin login----xxxxxxx");
            console.log(loginRes);
          },
          fail: (err) => {
            console.log("err", err);
          },
        });
      }
    },
    requestMatchList() {
      matchList({}).then((res) => {
        if (res.status == 1) {
          this.mathData = res.data || {};
        }
      });
    },
    requestActivityList() {
      activityList({}).then((res) => {
        if (res.status == 1) {
          this.taList = res.data || {};
        }
      });
    },
    requestShareInfo() {
      activityShareInfo({}).then((res) => {
        if (res.status == 1) {
          this.shareCountInfo = res.data || {};
        }
      });
    },
    requestActivityInfo() {
      activityInfo({}).then((res) => {
        if (res.status == 1) {
          this.activityInfoMap = res.data || {};
        }
      });
    },
    myRequestSubscribeMessage() {
      uni.requestSubscribeMessage({
        tmplIds: ["dG4qWOmB0VPOIljST52NEbUDWXRnMgKSjE3gZKWAqkI"],
        success(res) {},
      });
    },
    getShareTitle(userInfo) {
      var result = "Hi,我是";
      result += userInfo.nick_name || "";
      result += ",";
      if (userInfo.birthday.length > 0) {
        result += this.getTaAgesYear(userInfo.birthday);
        result += "·";
      }
      if (userInfo.height.length > 0) {
        result += userInfo.height;
        result += "cm·";
      }
      if (userInfo.job.length > 0) {
        result += userInfo.job;
        result += "·";
      }
      if (userInfo.education.length > 0) {
        result += userInfo.education;
        result += ",";
      }
      result += "喜欢我就加我好友吧!";
      return result;
    },
    getTaAgesYear(birthday) {
      let birthdayStr = birthday || "";
      if (birthdayStr == "") {
        return "";
      } else {
        return getAgesYear(birthdayStr);
      }
    },
    reloadData() {},
    // 获取微信右上角胶囊高度
    getHeight() {
      let res = wx.getMenuButtonBoundingClientRect();
      this.barstatusH = res.bottom + 8;
    },
    loginDismiss() {},
    timeup() {
      // 倒计时结束
      this.requestData();
    },
    decryptPhoneNumber(e) {
      let that = this;
      console.log("homewxcode---", this.wxcode);
      if (this.wxcode == "") {
        this.requrstWxCode();
        wxKeyManager.decryptPhoneNumber(e).then((res) => {
          console.log("wxkmanager-pohone---", res);
          that.loginSuccess();
        });
      } else {
        wxKeyManager.decryptPhoneNumberWithCode(e, this.wxcode).then((res) => {
          console.log("wxkmanager-pohone---", res);
          that.loginSuccess();
        });
      }
    },
    loginSuccess() {
      let that = this;
      that.isLogin = true;
      EventBus.$emit("loginSuccess", true);
      getInfo().then((res) => {
        if (res.status == 1) {
          uni.setStorageSync("userInfo", res.data || {});
          that.loginSuccessNext();
        }
      });
    },
    loginSuccessNext() {
      this.requestData();
      if (this.canNotZhuanfa()) {
        this.toHandleBaomingNext();
      }
    },
    canNotZhuanfa() {
      if (
        this.needComplete() ||
        this.needCustomized() ||
        this.needCertificated()
      ) {
        return true;
      }
      return false;
    },
    toHandleBaomingNext() {
      if (this.needComplete()) {
        this.toCompleteInformation();
        return;
      }
      if (this.needCustomized()) {
        this.toCustomized();
        return;
      }
      if (this.needCertificated()) {
        this.toCertificated();
        return;
      }
    },
    // 需要完善
    needComplete() {
      let userInfo = getApp().globalData.userInfo || {};
      let nickName = userInfo.nick_name || "";
      let height = userInfo.height || "";
      let work_place = userInfo.work_place || "";
      let job = userInfo.job || "";
      return nickName == "" || height == "" || work_place == "" || job == "";
    },
    // 去完善资料
    toCompleteInformation() {
      // 完善
      uni.navigateTo({
        url: "/pageA/my-profile/my-profile-once",
      });
    },
    // 需要定制
    needCustomized() {
      let userInfo = getApp().globalData.userInfo || {};
      let customized = userInfo.customized || false;
      return customized == false;
    },
    // 去定制
    toCustomized() {
      // 定制
      uni.navigateTo({
        url: "/pageA/my-profile/dingzhi?once=once",
      });
    },
    // 需要认证
    needCertificated() {
      let userInfo = getApp().globalData.userInfo || {};
      let certificated = userInfo.certificated || false;
      return certificated == false;
    },
    toCertificated() {
      // 认证
      uni.navigateTo({
        url: "/pageA/realName/realName?once=once",
      });
    },
    // zhuanbenye
    zhuanfaAction() {
      console.log("zhuanfaAction-----------");
      let that = this;
      activityShare({}).then((res) => {
        if (res.status == 1) {
          that.requestData();
          that.myRequestSubscribeMessage();
        }
      });
    },
    // 报名
    baomingAction() {
      console.log("baoming-----------");
      if (this.canNotZhuanfa()) {
        this.toHandleBaomingNext();
        return;
      }
      let that = this;
      activityApply({}).then((res) => {
        if (res.status == 1) {
          uni.showToast({
            icon: "none",
            title: "报名成功",
          });
		  that.refreshUserInfo();
          that.requestActivityInfo();
        }
      });
    },
    stopPaiAction() {
      let that = this;
      activityApply({ flag: false }).then((res) => {
        if (res.status == 1) {
          uni.showToast({
            icon: "none",
            title: "暂停派发成功",
          });
          that.requestData();
        }
      });
    },
    // 是否显示 为你匹配
    showMath() {
      let youlist = this.mathData.users || [];
      return youlist.length > 0;
    },
    getPaiCount() {
      let youlist = this.mathData.users || [];
      return youlist.length;
    },
  },
};
</script>

<style scoped lang="scss">
* {
  box-sizing: border-box;
}

page {
  background-color: #ffffff;
  min-height: 100vh;
}

.home-container {
  background-color: #ffffff;
  min-height: 100vh;

  .top-status-container {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 999999;
    width: 750rpx;
    box-sizing: content-box;
    background-color: white;
    // background-image: linear-gradient(159.23deg, #E4E9FF, rgba(229, 214, 255, 0) 77.36%);

    // .top-status {
    // 	position: relative;
    // 	left: 0;
    // 	top: 0;
    // 	width: 750rpx;
    // 	z-index: 999999;

    .bar-bg {
      width: 750rpx;
    }

    .top-status-paidx {
      width: 750rpx;
      bottom: 0rpx;
      height: 88rpx;
      display: flex;
      flex-direction: row;
      align-items: center;
      padding: 0 16px;
      z-index: 999999;
      position: absolute;

      .top-status-paidx-title {
        margin-left: 6px;
        font-weight: 400;
        font-size: 11px;
        line-height: 15px;
        color: #333333;
      }

      .paifa-icon {
        width: 137px;
        height: 22px;
      }
    }
  }

  .home-content-container {
  }
}

.safe-area-inset-bottom {
  padding-bottom: 120rpx;
  padding-bottom: calc(constant(safe-area-inset-bottom) + 120rpx);
  padding-bottom: calc(env(safe-area-inset-bottom) + 120rpx);
}
</style>
